package com.wkg;

public class Main {
    private static long count = 0L;

    public static void main12(String[] args) {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    System.out.println(Thread.currentThread().getName() + ": 我还活着");
                            Thread.sleep(1 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + ": 我即将死去");
        });
        System.out.println(Thread.currentThread().getName()
                + ": ID: " + thread.getId());
        System.out.println(Thread.currentThread().getName()
                + ": 名称: " + thread.getName());
        System.out.println(Thread.currentThread().getName()
                + ": 状态: " + thread.getState());
        System.out.println(Thread.currentThread().getName() + ": 优先级: " + thread.getPriority());
        System.out.println(Thread.currentThread().getName()
                + ": 后台线程: " + thread.isDaemon());
        System.out.println(Thread.currentThread().getName()
                + ": 活着: " + thread.isAlive());
        System.out.println(Thread.currentThread().getName()
                + ": 被中断: " + thread.isInterrupted());
        thread.start();
        while (thread.isAlive()) {
        }
        System.out.println(Thread.currentThread().getName()
                + ": 状态: " + thread.getState());
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 10; i++) {

                try {
                    System.out.println(Thread.currentThread().getName() + ": 我还活着");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + ": 我即将死去");
        });

        System.out.println("ID:" + thread.getId());
        System.out.println("优先级:" + thread.getPriority());
        System.out.println("是否后台线程:" + thread.isDaemon());
        System.out.println("是否存活:" + thread.isAlive());
        System.out.println("是否被中断:" + thread.isInterrupted());
        System.out.println("状态:" + thread.getState());
        thread.start();
        thread.join();
        Thread.sleep(1000);

        System.out.println("ID:" + thread.getId());
        System.out.println("优先级:" + thread.getPriority());
        System.out.println("是否后台线程:" + thread.isDaemon());
        System.out.println("是否存活:" + thread.isAlive());
        System.out.println("是否被中断:" + thread.isInterrupted());
        System.out.println("状态:" + thread.getState());

    }


    public static void main11(String[] args) throws InterruptedException {

        Thread thread01 = new Thread(() -> {
            for (long i = 0L; i < 5000000000L; i++) {
                //System.out.println(count);
                count++;
            }
        });
        Thread thread02 = new Thread(() -> {
            for (long i = 0L; i < 5000000000L; i++) {
                //System.out.println(count);
                count++;
            }
        });
        long startTime = System.currentTimeMillis();
        for (long i = 0; i < 10000000000L; i++) {
            //System.out.println(count);
            count++;
        }
/*        thread01.start();
        thread02.start();
        thread01.join();
        thread02.join();*/
        long endTime = System.currentTimeMillis();
        System.out.println("最终程序运行时长为:" + (endTime - startTime) + "ms");
    }


    public static void main3(String[] args) {
        Thread thread01 = new Thread(() -> {
            System.out.println("通过lamaba表达式创造线程...");
        });
        thread01.start();
    }


    public static void main2(String[] args) throws InterruptedException {
        Thread thread = new Thread() {
            @Override
            public void run() {
                System.out.println("通过Thread匿名内部类创建多线程");
            }
        };

        thread.setDaemon(true);
        thread.start();
        Thread.sleep(1000);
        System.out.println("主线程结束");
    }

    public static void main1(String[] args) {
        MyThread01 myThread = new MyThread01();
        myThread.start();
        MyRunnable01 myRunnable01 = new MyRunnable01();
        Thread thread = new Thread(myRunnable01);
        thread.start();


        while (true) {
            try {
                Thread.sleep(400);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("hello main thread...");
        }
    }
}


class MyRunnable01 implements Runnable {

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(400);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("hello run thread...");
        }
    }
}

class MyThread01 extends Thread {
    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(400);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("hello my thread...");
        }
    }
}